package order

import (
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"order_bff/controller"
	"order_bff/forms"
	"order_bff/global"
	"order_bff/model"
	"order_bff/order_proto"
	"strconv"
)

// 订单列表
func List(c *gin.Context) {
	//userId, _ := c.Get("userId")
	id := c.Query("user_id")
	userId, _ := strconv.Atoi(id)
	itemForm := forms.OrderListForm{}
	if err := c.ShouldBind(&itemForm); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}
	list, err := global.OrderSrvConnect.OrderList(context.Background(), &order_proto.OrderFilterRequest{
		UserId:  int32(userId),
		Page:    itemForm.Page,
		Limit:   itemForm.Limit,
		PayType: order_proto.OrderFilterRequest_PayTypes(itemForm.PayType),
		Status:  itemForm.Status,
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	var orderList []model.OrderInfo
	for _, v := range list.OrderInfo {
		orderList = append(orderList, model.OrderInfo{
			Id:      v.Id,
			UserId:  v.UserId,
			OrderSn: v.OrderSn,
			PayType: v.PayType,
			Status:  v.Status,
			Post:    v.Post,
			Total:   v.Total,
			Address: v.Address,
			Name:    v.Name,
			Mobile:  v.Mobile,
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "查询为空",
		"data": map[string]interface{}{
			"total":     list.Total,
			"orderList": orderList,
		},
	})
}

// 添加订单
func Add(c *gin.Context) {
	id := c.PostForm("user_id")
	userId, _ := strconv.Atoi(id)
	itemForm := forms.CreateOrderForm{}
	if err := c.ShouldBind(&itemForm); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}
	_, err := global.OrderSrvConnect.Create(context.Background(), &order_proto.OrderRequest{
		UserId:  int32(userId),
		Address: itemForm.Address,
		Name:    itemForm.Name,
		Mobile:  itemForm.Mobile,
		Post:    itemForm.Post,
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": nil,
	})
}

// 订单详情
func Detail(c *gin.Context) {
	userId := c.Query("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	id := c.Query("id")
	idInt, _ := strconv.Atoi(id)
	detail, err := global.OrderSrvConnect.OrderDetail(context.Background(), &order_proto.OrderRequest{
		ID:     int32(idInt),
		UserId: int32(userIdInt),
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	var orderInfo model.OrderInfo
	orderInfo.Id = detail.OrderInfo.Id
	orderInfo.OrderSn = detail.OrderInfo.OrderSn
	orderInfo.Post = detail.OrderInfo.Post
	orderInfo.UserId = detail.OrderInfo.UserId
	orderInfo.Address = detail.OrderInfo.Address
	orderInfo.Mobile = detail.OrderInfo.Mobile
	orderInfo.Name = detail.OrderInfo.Name
	orderInfo.Total = detail.OrderInfo.Total
	orderInfo.Status = detail.OrderInfo.Status
	orderInfo.PayType = detail.OrderInfo.PayType
	var orderGoods []model.OrderGoods
	for _, good := range detail.Goods {
		orderGoods = append(orderGoods, model.OrderGoods{
			OrderId:    int(good.OrderId),
			GoodsId:    int(good.GoodsId),
			GoodsName:  good.GoodsName,
			GoodsImage: good.GoodsImage,
			GoodsPrice: good.GoodsPrice,
			Nums:       int(good.Nums),
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": map[string]interface{}{
			"orderInfo":  orderInfo,
			"orderGoods": orderGoods,
		},
	})
}
